#include <qDebug>
#include <QThread>
#include <QProcess>
#include "logger.h"
#include "pingger.h"

PINGGER::PINGGER()
{

}

PINGGER::~PINGGER()
{

}

void PINGGER::init(std::string ip)
{
    pingIP = ip;
}

int PINGGER::get_ip_status()
{
    int status = PING_ERR;
    QProcess exc;
    QString cmdstr="ping -n 2 -w 100 " + QString(pingIP.c_str());

    exc.start(cmdstr);
    exc.waitForFinished(-1);
    QByteArray  cdata= exc.readAll();
    std::string tmpStr = std::string(cdata);

    if((tmpStr.find("TTL=")) != std::string::npos){
        status = PING_OK;
    }else{
        //LOG(INFO) << "cmdstr = " << cmdstr.toStdString() <<std::endl;
        LOG(ERROR) << "pingIP = " << pingIP << " ping err"<<std::endl;
        status = PING_ERR;
    }
    return status;
}

